<?php

namespace app\modules\m\controllers;

use yii\web\Controller;
use \app\common\services\PayOrderService;
use \app\models\pay\PayOrder;
use \app\models\book\Book;
use \app\modules\m\controllers\common\BaseController;
class PayController extends BaseController
{
    
    public function actionBuy()
    {
    	if(\Yii::$app->request->isPost){
    		$pay_order_id=$this->post("pay_order_id","");
            $status=$this->post("status","");
    		$res=PayOrder::find()->where(['id'=>$pay_order_id])->one();
    		if(!$res){
    			return $this->renderJson([],"非法操作0",-1);
    		}
            if($status==1){
                $res->status=$status;
                $res->updated_time=time();
                $res->save(0);
                return $this->renderJson(['url'=>'/user/order'],"支付成功~");
            }
    		if($status==0){
                //归还库存
                return PayOrderService::backStock($pay_order_id);
            }
    	}
    	$id=$this->get('pay_order_id',"");
    	$res=PayOrder::find()->where(['id'=>$id])->one();
    	if(!$res){
    		return $this->renderJson([],'非法操作-1',-1);
    	}
        return $this->render('buy',['info'=>$res]);
    }
    
}
